package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.SysRole;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.dto.SysRoleAddDTO;
import cn.com.dcsgo.dto.SysRoleQueryDTO;
import cn.com.dcsgo.dto.SysRoleUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.SysRoleService;
import cn.com.dcsgo.vo.SysRoleVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台系统角色管理相关接口
 *
 * @author Dcsgo
 * @since 2025/9/28 10:10
 */
@Validated
@RestController
@RequestMapping("sys/role")
@Tag(name = "后台系统角色管理相关接口")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 获取当前用户可查看的角色列表
     *
     * @return 当前用户可查看的角色列表
     */
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    @Operation(summary = "获取用户可查看的角色列表")
    public Result<List<SysRole>> getAllRoles() {
        List<SysRole> roleList = sysRoleService.getList();
        return Result.success(roleList);
    }

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:role:page')")
    @Operation(summary = "分页获取角色列表", description = "按创建时间升序排序")
    public Result<Page<SysRole>> getPage(PageDTO pageDTO, SysRoleQueryDTO sysRoleQueryDTO) {
        Page<SysRole> page = sysRoleService.getPage(pageDTO, sysRoleQueryDTO);
        return Result.success(page);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:save')")
    @Operation(summary = "添加角色")
    public Result<Void> addRole(@Validated @RequestBody SysRoleAddDTO sysRoleAddDTO) {
        int row = sysRoleService.save(sysRoleAddDTO);
        return Result.handle(row > 0);
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @Operation(summary = "删除角色", description = "可批量删除，将一并删除角色与权限（菜单）的关联关系")
    public Result<Void> deleteRole(@NotEmpty(message = "角色ID列表不能为空")
                                   @RequestBody List<Long> roleIds) {
        int row = sysRoleService.remove(roleIds);
        return Result.handle(row > 0);
    }

    @GetMapping("info/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    @Operation(summary = "获取角色详情", description = "包含其关联的菜单（权限）ID列表")
    public Result<SysRoleVO> getRoleInfo(@PathVariable Long roleId) {
        SysRoleVO sysRoleVO = sysRoleService.getRoleInfo(roleId);
        return Result.success(sysRoleVO);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:role:update')")
    @Operation(summary = "更新角色", description = "可一并更新角色与权限（菜单）的关联关系")
    public Result<Void> updateRole(@Validated @RequestBody SysRoleUpdateDTO sysRoleUpdateDTO) {
        int row = sysRoleService.update(sysRoleUpdateDTO);
        return Result.handle(row > 0);
    }

}
